---php---
<?php

namespace co2_OS;

$boot='---boot---'; # where boot files are

define('sysvar', '---sysvar---'); # where settings file is

/*-- stop blanks --*/
/*-- this is not the symbolic link --*/

define('co2_OS\cli_dir',getcwd());

foreach(scandir($boot) as $file)
if($file!='.'&&$file!='..')

{
	if(preg_match('/^function\.|class\.|constants\./',$file))
	require_once "$boot/$file";
}

trim(`whoami`)=='root'	or	exit('---
co2:
	failure: You are not root'.PHP_EOL);

spl_autoload_register('co2_OS\autoload');


new sysvar;
new aptitude;
new dependency;




require_once sysvar::get('lib').'/co2-api/'.sysvar::get('api').'/co2.php';

use \co2\v0_0_0 as api;

new api\runtime((object)array(
	'properties'=>
	array
	(
		'catch php errors'		=>	true
	),
	'error_handlers'=>
	array
	(
		function($level,$message,$file,$line,$context)
		{
			throw new api\Exception($message,null,$level,$file,$line,$context);
		}
	)
));

foreach(scandir($boot) as $file)
if($file!='.'&&$file!='..')

{
	if(preg_match('/^api\./',$file))
	require_once "$boot/$file";
}









if(!isset($argv[1]))
quit(ES_USAGE,' * Usage: co2 { install | uninstall | [run] $app }');

$A=$argv; array_shift($A);

$ARGS=parse_args($A);
	
try

{
	try
	{
		switch($action=$ARGS[0])
	
		{
			default:
		
				$app=strtolower($argv[1]);
			
				$run=run($app);

				if(isset($argv[2]))
			
				{
					$method=preg_replace('/-/','_',$argv[2]);
			
					for($args=array(),$array=null,$object=null,$object_key=null,$i=3; $i<$argc; $i++)
				
					{
						if($argv[$i]=='[')
						$array=array();
					
						elseif(is_array($array) && $argv[$i]!=']')
						$array[]=arg2var($argv[$i]);
					
						elseif($argv[$i]==']' && is_array($array))
					
						{
							$args[]=$array;
						
							$array=null;
						}
					
						elseif($argv[$i]=='{')
						$object=new \stdClass;
					
						elseif(is_object($object) && $argv[$i]!='}')
					
						{
							if(preg_match('/^--/',$argv[$i]) && is_null($object_key))
							$object_key=preg_replace('/^--/','',$argv[$i]);
						
							elseif(is_string($object_key))
						
							{
								$object->$object_key=arg2var($argv[$i]);
							
								$object_key=null;
							}
						}
					
						elseif(is_object($object) && $argv[$i]=='}')
					
						{
							$args[]=$object;
						
							$object=null;
						}
					
						else
						$args[]=arg2var($argv[$i]);
					}

					$run2=call_user_func_array(array($run,$method),$args);
				
					if(method_exists($run2,'__toString'))
					echo $run2.PHP_EOL;
				}
				else
				quit(ES_README,$run());
		
			break;
			
			case'install':
			
			break;
		
			case 'admin':
		
				$usage=' * Usage: co2 admin { aptitude | php }';
		
				if(!isset($argv[2]))
				quit(ES_USAGE,$usage);

				switch($action=$argv[2])
			
				{
					default:
				
						exit($usage);
				
					break;
					
					case'php':
					
						$php_bin=sysvar::get('php-bin');
					
						$usage='---
co2:
	failure: command incomplete
	usage: co2 admin php { info | ext }'.PHP_EOL;
	
						if(!isset($argv[3]))
						exit($usage);
						
						switch($argv[3])
						
						{
							case'info':
							
							if(isset($argv[4]))
							switch($argv[4])
							{
								default:
									$info='INFO_'.strtoupper($argv[4]);
								
									if(!defined($info))
									exit('---
	co2:
		failure: unknown info what'.PHP_EOL);
	
									$info=trim(`php -i "phpinfo(2);"`);
								break;
								
								case'modules':
								
									$info=trim(`$php_bin -m`);
								
								break;
							}
							else
							$info=trim(`php -i "phpinfo();"`);
							
								exit('---
co2:
	success: |
		'.$info.PHP_EOL);
							
							break;
							
							case'ext':
							
								$usage='---
co2:
	event: failure
	message: command incomplete
	usgae: co2 admin php ext { $ext | view | load | enable | disallow | unload }'.PHP_EOL;
	
								if(!isset($argv[4]))
								exit($usage);
								
								switch($argv[4])
								
								{
									case'enable':
									
										$usage='---
co2:
	failure: command incomplete
	usage: co2 admin php ext enable { $extension }'.PHP_EOL;
	
										if(!isset($argv[5]))
										exit($usage);
										
										$ext=preg_replace('/-/','_',$argv[5]);
										
										for($a=array(),$i=6;$i<$argc;$i++)
										$a[]=$argv[$i];
										
										php\ext::enable($ext,$a);
									
									break;
								}
							
							break;
						}
					
					break;
				
					case'aptitude':
				
						$usage='---
co2:
	event: failure
	message: command incomplete
	usage: co2 admin aptitude { list | lookup | dependencies | install | uninstall }'.PHP_EOL;
					
						if(!isset($argv[3]))
						exit($usage);
					
						switch($argv[3])
					
						{
							default:
						
								exit($usage);
						
							break;
						
							case'list':
						
								$i=aptitude::get();
						
								$yaml=array();
					
								$yaml['apps']=array();
							
								foreach($i as $_)
								if($_->type=='app')
								$yaml['apps'][$_->name]=$_->version;
							
								$yaml['lib']=array();
							
								foreach($i as $_)
								if($_->type=='lib')
								$yaml['lib'][$_->name]=$_->version;
				
								exit(run('yaml')->dump(array('co2'=>array(
									'success'=>$yaml
								))));
						
							break;
						
							case'dependencies':
						
								$dependencies=dependency::get();
							
								$r=array('app'=>array(),'lib'=>array());
							
								foreach($dependencies as $k=>$v)
							
								{
									$drug=explode(',',$k);
							
									switch($drug[0])
								
									{
										case'app':
									
											$r['app'][$drug[1]]=array();
									
										break;
									
										case'lib':
									
											$r['lib'][$drug[1]]=array($drug[2]=>array('app'=>array(),'lib'=>array()));
										
											foreach($v as $d)
										
											{
												$junkie=explode(',',$d);
											
												$r['lib'][$drug[1]][$drug[2]][$junkie[0]][$junkie[1]]=$junkie[2];
											}
									
										break;
									}
								
								}
							
								exit(run('yaml')->dump(array('co2'=>array(
									'event'=>'success',
									'message'=>$r
								))));
						
							break;
						
							case'install':
						
								$usage='---
co2:
	event: failure
	message: command incomplete
	usage: co2 admin aptitude install { app | lib }'.PHP_EOL;
					
								if(!isset($argv[4]))
								exit($usage);
					
								switch($format=$argv[4])
					
								{
									default:
						
										exit($usage);
						
									break;
						
									case'app':
						
										$usage='---
co2:
	event: failure
	message: command incomplete
	usage: co2 admin aptituted'.PHP_EOL;
						
										if(!isset($argv[5]) || !isset($argv[6]))
										exit($usage);
							
										install_app($app=$argv[5],$version=$argv[6]);
							
										exit("---
co2:
	event: success
	message:	App successfully installed
	app:	$app
	version:	$version".PHP_EOL);
						
									break;
						
									case'lib':
						
										$usage='---
co2:
	event: failure
	message: command incomplete
	usage: co2 admin aptitude install app $app $version'.PHP_EOL;
						
										if(!isset($argv[5]) || !isset($argv[6]))
										exit($usage);
							
										install_lib($lib=$argv[5],$version=$argv[6]);
							
										exit("---
co2:
	event: success
	message:	Library successfully installed
	lib:	$lib
	version:	$version".PHP_EOL);
						
									break;
								}
						
							break;
						
							case'uninstall':
						
								$usage='---
co2:
	event: failure
	message: command incomplete
	usage: co2 admin aptitude uninstall { app | lib }'.PHP_EOL;
							
								if(!isset($argv[4]))
								exit($usage);
					
								switch($format=$argv[4])
					
								{
									default:
						
										exit($usage);
						
									break;
						
									case'app':
						
										$usage='---
co2:
	event: failure
	message: command incomplete
	usage: co2 admin aptitude uninstall app $app $version'.PHP_EOL;
						
										if(!isset($argv[5])||!isset($argv[6]))
										exit($usage);
							
										uninstall_app($app=$argv[5],$version=$argv[6]);
							
										exit("---
co2:
	event: success
	message:	App successfully uninstalled
	app:	$app
	version:	$version".PHP_EOL);
						
									break;
						
									case'lib':
						
										$usage='---
co2:
	event: failure
	message: command incomplete
	usage: co2 admin aptitude install app $app $version'.PHP_EOL;
						
										if(!isset($argv[5]) || !isset($argv[6]))
										exit($usage);
							
										uninstall_lib($lib=$argv[5],$version=$argv[6]);
							
										exit("---
co2:
	event: success
	message:	Library successfully uninstalled
	lib:	$lib
	version:	$version".PHP_EOL);
						
									break;
								}
						
							break;
						}
				}
		
			break;
		}
	}
	catch(api\Exception $e)
	
	{
		quit($e->getLevel(),$e->getMessage().'
'.$e->getFile().'@'.$e->getLine(),(object)array('trace'=>$e->getTraceAsString(),'type'=>'xml'));
	}
}

catch(\Exception $e)

{
	quit(ES_EXCEPTION,'<exception coo>
  <message><![CDATA[
    '.$e->getMessage().'
  ]]></message>
  <file>'.$e->getFile().'</file>
  <line>'.$e->getLine().'</line>
</exception>',(object)array('trace'=>$e->getTraceAsString(),'type'=>'xml'));
}
